package main

import (
	"fmt"
	"os"

	"gitee.com/go-course/go8/projects/script_exporter/collector/rocketmq/collector"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/common/expfmt"
)

func main() {
	// 新注册表
	registry := prometheus.NewRegistry()

	// 注册RocketMQ collector, 允许通过环境变量来传递DataDir
	dataDir := os.Getenv("ROCKETMQ_DATA_DIR")
	if dataDir == "" {
		dataDir = "collector/sample/data.txt"
	}

	c := collector.NewCollector(dataDir)
	registry.MustRegister(c)

	// 执行采集, 获取一个组metric
	mf, err := registry.Gather()
	if err != nil {
		fmt.Println(err)
		os.Exit(2)
	}

	// 打印这些Metric 编码输出到 标准输出
	enc := expfmt.NewEncoder(os.Stdout, expfmt.FmtText)
	// 遍历每一个metric 把他编码成FmtText, 也就是Prometheus文本格式
	// 编码过后会把结果输出到buffer里面
	for i := range mf {
		enc.Encode(mf[i])
	}
}
